async function ajax(url, { headers, body, method = 'GET', ...otherParams } = {}) {
  if (!headers) {
    if (typeof Headers !== 'undefined') {
      headers = new Headers({
        'Content-Type': 'application/json'
      });
    } else {
      headers = {
        'Content-Type': 'application/json'
      };
    }
  }
  let data = body;
  if (typeof data === 'object') {
    data = JSON.stringify(data);
  }
  let response = await fetch(url, { headers, data, method, credentials: 'omit', ...otherParams });
  if (response.ok) {
    // try {
    //   var res = await response.json();
    //   return res;
    // } catch (e) {
    var res = await response.text();
    try {
      return JSON.parse(res);
    } catch (error) {
      return res;
    }
    // }
  }
  return response.statusText;
}

const obj = {
  methods: ['get', 'post', 'options', 'delete', 'put']
};
const fetch = require("node-fetch");

const axios = new Proxy(obj, {
  get: function (target, prop) {
    if (!target.methods.includes(prop.toLowerCase())) {
      throw new Error('not implement!');
    }
    return function (url, options = {}) {
      return ajax(url, {
        ...options,
        method: prop.toUpperCase()
      });
    };
  }
});

ajax.test = function () {
  console.log('-------test--');
};

(async function () {
  let res = await axios.get('http://www.baidu.com');
  console.log(res);
  ajax.test();
})();

// async function post(url, data, config = {}) {
//   return ajax(url, { method: 'post', body: data, ...config });
// }

// async function get(url, data, config = {}) {
//   return ajax(url, { method: 'get', body: data, ...config });
// }

// (async function () {
//   const url = 'http://192.168.10.12:10011/http/rest/menjin';
//   // let result = await post(url, {});
//   let result = await ajax(url, { method: 'post', body: {} });
//   console.log(result);
// })();
